<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-11-13
 * Time: 16:35:04
 * Info:
 */

namespace app\common\model;

use think\facade\Db;
use Exception;

class ContentExt extends TimeInit
{

    protected $pk = 'ext_id';

    protected $name = 'content_ext';

    public function isExistTable($table)
    {
        $res = Db::query('SHOW TABLES LIKE '."'".$table."'");
        if ($res) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断字段是否存在
     *
     * @param $field
     */
    public function isExistField($field)
    {
        $prefix    = config('database.connections.mysql.prefix');
        $tableName = $prefix.$this->name;
        $isTable   = $this->isExistTable($tableName);
        if ( ! $isTable) {
            return ['code' => 0, 'msg' => '数据表不存在'];
        }
        //查询表是否存在
        $sql = "select count(*) from information_schema.COLUMNS where table_name='{$tableName}' and column_name='".$field."'";
        $res = Db::query($sql);
        if ($res[0]['count(*)'] != 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 添加字段
     *
     * @param $table
     * @param $field
     *
     * @return void
     */
    public function createField($field, $type, $mysql, $description = '')
    {
        $prefix    = config('database.connections.mysql.prefix');
        $tableName = $prefix.$this->name;

        try {
            if ($type == 9) {
                //时间datetime类型
                $sql = "ALTER TABLE {$tableName} ADD {$field} {$mysql} COMMENT '{$description}' ";
            } else {
                $sql = "ALTER TABLE {$tableName} ADD {$field} {$mysql} DEFAULT '' COMMENT '{$description}' ";
            }
            //添加字段
            Db::query($sql);
            //添加索引
            //Db::query("create index content_{$field}_index on {$tableName} ($field)");
        } catch (\Exception $e) {

        }

        return true;
    }

    /**
     * 编辑字段
     *
     * @param $table
     * @param $field 新字段名
     *
     * @return void
     */
    public function updateField($field, $mysql, $description = '')
    {
        $prefix    = config('database.connections.mysql.prefix');
        $tableName = $prefix.$this->name;

        try {
            //添加字段
            Db::query("ALTER TABLE {$tableName} CHANGE {$field} {$field} {$mysql} NOT NULL DEFAULT '' COMMENT '{$description}' ");
            //添加索引
            //Db::query("create index content_{$field}_index on {$tableName} ($field)");
        } catch (\Exception $e) {

        }

        return true;
    }

    /**
     * 编辑字段
     *
     * @param $table
     * @param $field 新字段名
     *
     * @return void
     */
    public function deleteField($field)
    {
        $prefix    = config('database.connections.mysql.prefix');
        $tableName = $prefix.$this->name;

        try {
            //添加字段
            Db::query("ALTER TABLE {$tableName} DROP {$field} ");
            //添加索引
            //Db::query("create index content_{$field}_index on {$tableName} ($field)");
        } catch (\Exception $e) {

        }

        return true;
    }
}
